<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="养老院id" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入养老院id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择类别(1：未验证，2：已验证)" clearable>
          <el-option
            v-for="dict in dict.type.verify_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="微信" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入微信openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="是否入住" prop="useful">
        <el-select v-model="queryParams.useful" placeholder="请选择是否入住 0-未入住 1-已入住" clearable>
          <el-option
            v-for="dict in dict.type.check_in_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:personInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:personInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:personInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:personInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户id" align="center" prop="personId" />
      <!-- <el-table-column label="养老院id" align="center" prop="communityId" /> -->
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="身份证号" align="center" prop="cardNo" />
      <el-table-column label="医保卡号" align="center" prop="medicalCard" />
      <el-table-column label="紧急联系人" align="center" prop="emergentContact" />
      <el-table-column label="紧急联系人电话" align="center" prop="contactMobile" />
      <!-- <el-table-column label="人脸图片" align="center" prop="faceUrl" /> -->
      <el-table-column label="类别" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.verify_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="身份证图片正面" align="center" prop="cardPic1" />
      <el-table-column label="身份证图片背面" align="center" prop="cardPic2" /> -->
      <el-table-column label="家庭地址" align="center" prop="addr" />
      <!-- <el-table-column label="微信openid" align="center" prop="openId" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否入住" align="center" prop="useful">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_in_state" :value="scope.row.useful"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:personInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:personInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="养老院id" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入养老院id" />
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="医保卡号" prop="medicalCard">
          <el-input v-model="form.medicalCard" placeholder="请输入医保卡号" />
        </el-form-item>
        <el-form-item label="紧急联系人" prop="emergentContact">
          <el-input v-model="form.emergentContact" placeholder="请输入紧急联系人" />
        </el-form-item>
        <el-form-item label="紧急联系人电话" prop="contactMobile">
          <el-input v-model="form.contactMobile" placeholder="请输入紧急联系人电话" />
        </el-form-item>
        <el-form-item label="人脸图片" prop="faceUrl">
          <el-input v-model="form.faceUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="类别(1：未验证，2：已验证)" prop="state">
          <el-select v-model="form.state" placeholder="请选择类别(1：未验证，2：已验证)">
            <el-option
              v-for="dict in dict.type.verify_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证图片正面" prop="cardPic1">
          <el-input v-model="form.cardPic1" placeholder="请输入身份证图片正面" />
        </el-form-item>
        <el-form-item label="身份证图片背面" prop="cardPic2">
          <el-input v-model="form.cardPic2" placeholder="请输入身份证图片背面" />
        </el-form-item>
        <el-form-item label="家庭地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入家庭地址" />
        </el-form-item>
        <el-form-item label="微信openid" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入微信openid" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否入住 0-未入住 1-已入住" prop="useful">
          <el-select v-model="form.useful" placeholder="请选择是否入住 0-未入住 1-已入住">
            <el-option
              v-for="dict in dict.type.check_in_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPersonInfo, getPersonInfo, delPersonInfo, addPersonInfo, updatePersonInfo } from "@/api/user/personInfo";

export default {
  name: "PersonInfo",
  dicts: ['verify_state', 'check_in_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老人信息表格数据
      personInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        userName: null,
        mobile: null,
        sex: null,
        cardNo: null,
        state: null,
        openId: null,
        useful: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询老人信息列表 */
    getList() {
      this.loading = true;
      listPersonInfo(this.queryParams).then(response => {
        this.personInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        personId: null,
        communityId: null,
        userName: null,
        mobile: null,
        password: null,
        sex: null,
        age: null,
        cardNo: null,
        medicalCard: null,
        emergentContact: null,
        contactMobile: null,
        faceUrl: null,
        state: null,
        cardPic1: null,
        cardPic2: null,
        addr: null,
        openId: null,
        createTime: null,
        remark: null,
        useful: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.personId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加老人信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const personId = row.personId || this.ids
      getPersonInfo(personId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老人信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.personId != null) {
            updatePersonInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPersonInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const personIds = row.personId || this.ids;
      this.$modal.confirm('是否确认删除老人信息编号为"' + personIds + '"的数据项？').then(function() {
        return delPersonInfo(personIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/personInfo/export', {
        ...this.queryParams
      }, `personInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
